﻿var adinfo = {
    advpostionid: "",
    pageid: "",
    userid:"",
    adcss:"at"
}
var win = window;
var dom = win.document;
//广告业务配置对象
adconfig = { 
    //外部配置项
configitem: [{ name: "autosense_view_adids", val: "" }, { name: "autosense_view_adpositions", val: "" }, { name: "autosense_view_keywordids", val: "" }, { name: "autosense_view_groupindexes", val: "" }, { name: "autosense_view_unionid", val: "" }, { name: "autosense_view_adsiteid", val: "" }, { name: "autosense_view_addomainid", val: "" }, { name: "autosense_view_url", val: "" }, { name: "autosense_view_ip", val: "" }, { name: "autosense_view_areacodeid", val: "" }, { name: "autosense_view_hitkeywordids", val: "" }, { name: "autosense_view_hitkeywordpids", val: "" }],
    kv: new Object,
    //载入配置
    loadConfig: function () {
        var configitems = adconfig.configitem;
        for (var i = 0; i < configitems.length; i++) {
            if (win[configitems[i].name]) {
                adconfig.configitems[i].val = win[configitems[i].name];
                adconfig.kv[configitems[i].name] = adconfig.configitems[i].val;               
            };
        };
    }
};
function addEvent(domobj, eventType, handler, argsObject) {
    var eventHandler = handler;
    if (argsObject) {
        eventHandler = function (e) {
            handler.call(argsObject, e);
            return true;
        };
    };
    if (window.attachEvent)
        domobj.attachEvent("on" + eventType, eventHandler);
    else
        domobj.addEventListener(eventType, eventHandler, false);
};
function clickadv(ev) {
    // alert("ie6 is ok");
    var url = "";
    if (!this.original) {
        this.original = this.href;
    };
    url = this.original;//this指向点击的a标签
    ev = ev || window.event;
    var mx, my;
    if (ev.pageX || ev.pageY) {
        mx = ev.pageX, my = ev.pageY
    } else {
        mx = ev.clientX + document.body.scrollLeft - document.body.clientLeft;
        my = ev.clientY + document.body.scrollTop - document.body.clientTop;
    }
    url = url + "&mx=" + mx + "&my=" + my;
    this.href = url;
   // alert(url);
};
var _$c = function (cssname) {
    if (dom.getElementsByClassName)
        return dom.getElementsByClassName(cssname);
    else {
        var all = dom.all;
        var objs = new Array;
        for (var i = 0; i < all.length; i++) {
            if (all[i].className == cssname)
                objs.push(all[i]);
        };
        if (objs.length == 0)
            objs = null;
        return objs;
    };
};
function adBindEvent() {
    //var advs = _$c(adinfo.adcss);
    var advs = dom.getElementsByTagName("A");
    if (advs) {
        for (var i = 0; i < advs.length; i++) {
            if (advs[i]) {
                addEvent(advs[i], "click", clickadv, advs[i]);
            };
        };
    };
};
function getAdPositionInfo() {
    var pairs = location.search.substring(1).split("&");
    var count = 0;
    for (var i = 0; i < pairs.length; i++) {
        if (count == 2) break;
        var pos = pairs[i].indexOf('=');
        if (pos == -1) continue;
        var name = pairs[i].substring(0, pos);
        if (name == "posid") {
            adinfo.advpostionid = pairs[i].substring(pos + 1);
            count++;
            continue;
        };
        if (name == "pgid") {
            adinfo.pageid = pairs[i].substring(pos + 1);
            count++;
            continue;
        };
        if (name == "userid") {
            adinfo.userid = pairs[i].substring(pos + 1);
            count++;
            continue;
        };
    };
};
function getxy(domobj) {
    var x = 0, y = 0;
    if (domobj.getBoundingClientRect) {
        var box = domobj.getBoundingClientRect();
        var D = document.documentElement;
        x = box.left + Math.max(D.scrollLeft, document.body.scrollLeft) - D.clientLeft;
        y = box.top + Math.max(D.scrollTop, document.body.scrollTop) - D.clientTop
    }
    else {
        for (; domobj != document.body; x += domobj.offsetLeft, y += domobj.offsetTop, domobj = domobj.offsetParent) { }
    }
    return { x: x, y: y };
};
//配置脚本执行过程
var actionconfig = [{ name: "initadconfig", ev: getAdPositionInfo, isaction: true }, { name: "bindEvent", ev: adBindEvent, isaction: true }];
//执行
for (var runindex = 0; runindex < actionconfig.length; runindex++) {
    if (win[actionconfig[runindex].name])
        actionconfig[runindex].isaction = win[actionconfig[runindex].name];
    if (actionconfig[runindex].isaction)
        actionconfig[runindex].ev();
}